package com.fr.schedule.output;

import com.fr.base.ReflectionUtils;
import com.fr.base.XMLable;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.ResultWorkBook;
import com.fr.report.io.ExcelExporter;
import com.fr.report.io.PDFExporter;
import com.fr.report.io.TemplateExporter;
import com.fr.report.io.WordExporter;
import com.fr.schedule.ScheduleContext;
import com.fr.schedule.dao.FieldMapper;
import com.fr.schedule.dao.IdFieldMapper;
import com.fr.schedule.dao.JdbcDaoManager;
import com.fr.schedule.dao.ObjectMapper;
import com.fr.schedule.dao.ValueFieldMapper;
import com.fr.schedule.util.ScheduleConstants;
import com.fr.web.platform.entry.FileEntry;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class ScheduledOutput
  implements XMLable
{
  public static final String XML_TAG = "ScheduleOutput";
  public static final String TABLE_NAME = "fr_schedule_output";
  private long id = -1L;
  private String baseFileName;
  private String description;
  private long folderEntryId = -1L;
  private OutputFileFormat outputFileFormat;
  private EmailNotification emailNotification;
  private FileRepository fileRepository = new FileRepository();
  public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper("fr_schedule_output", new FieldMapper[] { new FieldMapper("id", 4, "id"), new FieldMapper("baseFileName", 12, "base_name"), new FieldMapper("description", 12, "description"), new FieldMapper("folderEntryId", 4, "folderEntryId"), new ValueFieldMapper("outputFileFormat", 4, "format")
  {
    public Object value2Field(Object paramObject)
    {
      if (paramObject == null)
        return new OutputFileFormat();
      OutputFileFormat localOutputFileFormat = new OutputFileFormat();
      localOutputFileFormat.setFormat(((Number)paramObject).intValue());
      return localOutputFileFormat;
    }

    public Object field2Value(Object paramObject)
    {
      if (paramObject == null)
        return new Integer(new OutputFileFormat().getFormat());
      return ReflectionUtils.getPrivateFieldValue(paramObject, "format");
    }
  }
  , new IdFieldMapper(EmailNotification.class, "emailNotification", -5, "emailId") });

  public ScheduledOutput()
  {
  }

  public ScheduledOutput(String paramString1, String paramString2, FileRepository paramFileRepository, OutputFileFormat paramOutputFileFormat, EmailNotification paramEmailNotification)
  {
    this.baseFileName = paramString1;
    this.description = paramString2;
    this.fileRepository = paramFileRepository;
    this.outputFileFormat = paramOutputFileFormat;
    this.emailNotification = paramEmailNotification;
  }

  public void setId(long paramLong)
  {
    this.id = paramLong;
  }

  public long getId()
  {
    return this.id;
  }

  public JSONObject createJSONConfig()
    throws JSONException
  {
    JSONObject localJSONObject1 = new JSONObject();
    localJSONObject1.put("id", getId());
    localJSONObject1.put("baseFileName", getBaseFileName());
    localJSONObject1.put("description", getDescription());
    if (getFolderEntryId() > -1L)
      localJSONObject1.put("folderEntryId", "0" + getFolderEntryId());
    JSONObject localJSONObject2 = getOutputFileFormat().createJSONConfig();
    localJSONObject1.put("outputFileFormat", localJSONObject2);
    if (getEmailNotification() != null)
    {
      JSONObject localJSONObject3 = getEmailNotification().createJSONConfig();
      localJSONObject1.put("emailNotification", localJSONObject3);
    }
    return localJSONObject1;
  }

  public static ScheduledOutput analyzeJSON(JSONObject paramJSONObject)
  {
    ScheduledOutput localScheduledOutput;
    try
    {
      localScheduledOutput = null;
      if (paramJSONObject.length() == 0)
        return new ScheduledOutput();
      if (paramJSONObject.has("id"))
        localScheduledOutput = (ScheduledOutput)ScheduleContext.getJdbcDaoManager().load(ScheduledOutput.class, paramJSONObject.getLong("id"));
      else
        localScheduledOutput = new ScheduledOutput();
      String str1 = paramJSONObject.getString("baseFileName");
      String str2 = paramJSONObject.getString("description");
      long l = -1L;
      if (paramJSONObject.has("folderEntryId"))
        l = paramJSONObject.getLong("folderEntryId");
      JSONObject localJSONObject1 = paramJSONObject.getJSONObject("outputFileFormat");
      OutputFileFormat localOutputFileFormat = OutputFileFormat.analyzeJSON(localJSONObject1);
      EmailNotification localEmailNotification = null;
      if (paramJSONObject.has("emailNotification"))
      {
        JSONObject localJSONObject2 = paramJSONObject.getJSONObject("emailNotification");
        localEmailNotification = EmailNotification.analyzeJSON(localJSONObject2);
      }
      localScheduledOutput.setBaseFileName(str1);
      localScheduledOutput.setDescription(str2);
      localScheduledOutput.setFolderEntryId(l);
      localScheduledOutput.setOutputFileFormat(localOutputFileFormat);
      localScheduledOutput.setEmailNotification(localEmailNotification);
      return localScheduledOutput;
    }
    catch (JSONException localJSONException)
    {
      localJSONException.printStackTrace();
    }
    return null;
  }

  public File[] exportReportFiles(ResultWorkBook paramResultWorkBook, String paramString, File paramFile)
    throws Exception
  {
    File localFile;
    FileOutputStream localFileOutputStream;
    FileEntry localFileEntry;
    if (this.outputFileFormat == null)
      return new File[0];
    ArrayList localArrayList = new ArrayList();
    if (this.outputFileFormat.isConvertToCpt())
    {
      localFile = generateUnExsitFile(paramFile, paramString, ".cpt");
      localFileOutputStream = new FileOutputStream(localFile);
      new TemplateExporter().export(localFileOutputStream, paramResultWorkBook);
      localFileOutputStream.flush();
      localFileOutputStream.close();
      localArrayList.add(localFile);
      if (this.folderEntryId > -1L)
      {
        localFileEntry = new FileEntry(this.baseFileName + ".cpt", this.description, localFile.getAbsolutePath());
        localFileEntry.setParentId(this.folderEntryId);
        ScheduleContext.getJdbcDaoManager().saveOrUpdate(localFileEntry);
      }
    }
    if (this.outputFileFormat.isConvertToExcel())
    {
      localFile = generateUnExsitFile(paramFile, paramString, ".xls");
      localFileOutputStream = new FileOutputStream(localFile);
      new ExcelExporter(null).export(localFileOutputStream, paramResultWorkBook);
      localFileOutputStream.flush();
      localFileOutputStream.close();
      localArrayList.add(localFile);
      if (this.folderEntryId > -1L)
      {
        localFileEntry = new FileEntry(this.baseFileName + ".xls", this.description, localFile.getAbsolutePath());
        localFileEntry.setParentId(this.folderEntryId);
        ScheduleContext.getJdbcDaoManager().saveOrUpdate(localFileEntry);
      }
    }
    if (this.outputFileFormat.isConvertToPdf())
    {
      localFile = generateUnExsitFile(paramFile, paramString, ".pdf");
      localFileOutputStream = new FileOutputStream(localFile);
      new PDFExporter().export(localFileOutputStream, paramResultWorkBook);
      localFileOutputStream.flush();
      localFileOutputStream.close();
      localArrayList.add(localFile);
      if (this.folderEntryId > -1L)
      {
        localFileEntry = new FileEntry(this.baseFileName + ".pdf", this.description, localFile.getAbsolutePath());
        localFileEntry.setParentId(this.folderEntryId);
        ScheduleContext.getJdbcDaoManager().saveOrUpdate(localFileEntry);
      }
    }
    if (this.outputFileFormat.isConvertToWord())
    {
      localFile = generateUnExsitFile(paramFile, paramString, ".doc");
      localFileOutputStream = new FileOutputStream(localFile);
      new WordExporter().export(localFileOutputStream, paramResultWorkBook);
      localFileOutputStream.flush();
      localFileOutputStream.close();
      localArrayList.add(localFile);
      if (this.folderEntryId > -1L)
      {
        localFileEntry = new FileEntry(this.baseFileName + ".doc", this.description, localFile.getAbsolutePath());
        localFileEntry.setParentId(this.folderEntryId);
        ScheduleContext.getJdbcDaoManager().saveOrUpdate(localFileEntry);
      }
    }
    return ((File[])localArrayList.toArray(new File[localArrayList.size()]));
  }

  public void setBaseFileName(String paramString)
  {
    this.baseFileName = paramString;
  }

  public String getBaseFileName()
  {
    return this.baseFileName;
  }

  public void setDescription(String paramString)
  {
    this.description = paramString;
  }

  public String getDescription()
  {
    return this.description;
  }

  public void setFolderEntryId(long paramLong)
  {
    this.folderEntryId = paramLong;
  }

  public long getFolderEntryId()
  {
    return this.folderEntryId;
  }

  public void setFileRepository(FileRepository paramFileRepository)
  {
    this.fileRepository = paramFileRepository;
  }

  public FileRepository getFileRepository()
  {
    return this.fileRepository;
  }

  public void setOutputFileFormat(OutputFileFormat paramOutputFileFormat)
  {
    this.outputFileFormat = paramOutputFileFormat;
  }

  public OutputFileFormat getOutputFileFormat()
  {
    return this.outputFileFormat;
  }

  public void setEmailNotification(EmailNotification paramEmailNotification)
  {
    if ((this.id != 0L) && (this.emailNotification != null))
    {
      if ((paramEmailNotification != null) && (this.emailNotification.getId() == paramEmailNotification.getId()))
        break label72:
      ScheduleContext.getJdbcDaoManager().deleteById(EmailNotification.class, this.emailNotification.getId());
    }
    label72: this.emailNotification = paramEmailNotification;
  }

  public EmailNotification getEmailNotification()
  {
    return this.emailNotification;
  }

  private static File generateUnExsitFile(File paramFile, String paramString1, String paramString2)
  {
    for (File localFile = new File(paramFile, paramString1 + "_" + DateUtils.getDate2AllIncludeSSS(new Date()) + "_" + ScheduleConstants.RANDOM.nextInt(1000) + paramString2); localFile.exists(); localFile = new File(paramFile, paramString1 + "_" + DateUtils.getDate2AllIncludeSSS(new Date()) + "_" + ScheduleConstants.RANDOM.nextInt(1000) + paramString2));
    return localFile;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isAttr())
    {
      String str;
      if ((str = paramXMLableReader.getAttr("baseFileName")) != null)
        setBaseFileName(str);
      if ((str = paramXMLableReader.getAttr("description")) != null)
        setDescription(str);
    }
    else if (paramXMLableReader.isChildNode())
    {
      if ("FileRepository".equals(paramXMLableReader.getTagName()))
        this.fileRepository = ((FileRepository)paramXMLableReader.readXMLObject(new FileRepository()));
      else if ("OutputFileFormat".equals(paramXMLableReader.getTagName()))
        this.outputFileFormat = ((OutputFileFormat)paramXMLableReader.readXMLObject(new OutputFileFormat()));
      else if ("EmailNotification".equals(paramXMLableReader.getTagName()))
        this.emailNotification = ((EmailNotification)paramXMLableReader.readXMLObject(new EmailNotification()));
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("ScheduleOutput").attr("baseFileName", this.baseFileName).attr("description", this.description);
    if (this.fileRepository != null)
      this.fileRepository.writeXML(paramXMLPrintWriter);
    if (this.outputFileFormat != null)
      this.outputFileFormat.writeXML(paramXMLPrintWriter);
    if (this.emailNotification != null)
      this.emailNotification.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.end();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof ScheduledOutput))
      return false;
    ScheduledOutput localScheduledOutput = (ScheduledOutput)paramObject;
    return ((ComparatorUtils.equals(this.baseFileName, localScheduledOutput.baseFileName)) && (ComparatorUtils.equals(this.description, localScheduledOutput.description)) && (ComparatorUtils.equals(this.fileRepository, localScheduledOutput.fileRepository)) && (ComparatorUtils.equals(this.outputFileFormat, localScheduledOutput.outputFileFormat)) && (ComparatorUtils.equals(this.emailNotification, localScheduledOutput.emailNotification)));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    ScheduledOutput localScheduledOutput = (ScheduledOutput)super.clone();
    if (this.fileRepository != null)
      localScheduledOutput.setFileRepository((FileRepository)this.fileRepository.clone());
    if (this.outputFileFormat != null)
      localScheduledOutput.setOutputFileFormat((OutputFileFormat)this.outputFileFormat.clone());
    if (this.emailNotification != null)
      localScheduledOutput.setEmailNotification((EmailNotification)this.emailNotification.clone());
    return localScheduledOutput;
  }
}